package io.gitee.thinkbungee.exception.advice;

import io.gitee.thinkbungee.bean.ResponseResult;
import io.gitee.thinkbungee.constant.SatelliteConstant;
import io.gitee.thinkbungee.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @author chenyichang
 * @since 2022/5/27
 */
@RestControllerAdvice
@Slf4j
public class ExceptionHandlerAdvice {


    /**
     * 业务异常处理
     *
     * @param e 异常
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseResult handleBusinessException(BusinessException e) {
        return ResponseResult.builder()
                .code(e.getCode())
                .args(e.getArgs())
                .build();
    }


    /**
     * 通用异常
     *
     * @param e 异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult exception(Exception e) {
        log.error("系统异常", e);
        return ResponseResult.builder()
                .code(SatelliteConstant.HttpCode.FAIL_CODE)
                .args(new String[]{e.getMessage()})
                .message("系统异常")
                .build();
    }
}
